package org.hfy.calc;

import org.hfy.Constants;
import org.hfy.collections.helper.NonTerminator;
import org.hfy.collections.helper.Production;
import org.hfy.collections.helper.Symbol;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @Author: Takagi
 * @date: 2023/5/28
 */
public class LLState {
    public Map<Symbol, Production> map;

    public Production get(Symbol key) {
        return map.get(key);
    }

    public LLState() {
        map = new HashMap<>();
    }

    public void addAll(Set<Symbol> symbols, Production production) {
        for (Symbol symbol : symbols) {
            add(symbol, production);
        }
    }

    public void add(Symbol symbol, Production production) {
        if (Constants.isEpsilon(symbol)) return;
        map.put(symbol, production);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        map.forEach((symbol, production) -> {
            sb.append(String.format("%s : %s\n", symbol.content, production.toString()));
        });
        return sb.toString();
    }
}
